package logic;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import constants.PercentNumber;
import database.DataConnection;

/** ConfigSaver class.
 * @author Jesus
 *
 */
public class BrickBreakerConfig {
	
	/** Writes all the parameters into the BBCONFIG table.
	 * @param userName : The current logged user's nick name.
	 * @param sound : A Boolean with a true if the music is enabled or false otherwise.
	 * @param livesNumber : An Integer with the selected number of lives.
	 * @param ammoNumber : An Integer with the selected number of Ammunition.
	 * @param rowNumber : An Integer with the selected number of Rows.
	 * @param columnNumber : An Integer with the selected number of Columns.
	 * @param ballSpeed : An Integer with the selected Ball's speed.
	 * @param batSpeed : An Integer with the selected Bat's speed.
	 * @param bonusSpeed : An Integer with the selected Bonus speed.
	 * @param percent : A percent enum with the selected percent of the Bonus creation.
	 */
	public static void write(String userName, Boolean sound, Integer livesNumber, Integer ammoNumber, 
			Integer rowNumber, Integer columnNumber, Integer ballSpeed, Integer batSpeed, Integer bonusSpeed, PercentNumber percent) {
		Integer intSound = sound ? 1 : 0;
		try {
			Connection connection = DataConnection.openConection();
			PreparedStatement prepStatement = connection.prepareStatement("Select * from BBCONFIG where nick = ?;");
			prepStatement.setString(1, userName);
			ResultSet resultSet = prepStatement.executeQuery();
			if(resultSet.next()) {
				resultSet.close();
				prepStatement.close();
				//Overwrites the user BBConfig.
				prepStatement = connection.prepareStatement("Update BBCONFIG set sound = ?, liveNumber = ?, ammoNumber = ?, rowNumber = ?," +
						" columnNumber = ?, ballSpeed = ?, batSpeed = ?, bonusSpeed = ?, percent = ? where nick = ?;");
				prepStatement.setString(1, intSound.toString());
				prepStatement.setString(2, livesNumber.toString());
				prepStatement.setString(3, ammoNumber.toString());
				prepStatement.setString(4, rowNumber.toString());
				prepStatement.setString(5, columnNumber.toString());
				prepStatement.setString(6, ballSpeed.toString());
				prepStatement.setString(7, batSpeed.toString());
				prepStatement.setString(8, bonusSpeed.toString());
				prepStatement.setString(9, percent.percentNumberToInteger().toString());
				prepStatement.setString(10, userName);
			} else {
				resultSet.close();
				prepStatement.close();
				//Creates new user BBConfig.
				prepStatement = connection.prepareStatement("Insert into BBCONFIG values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?);");
				prepStatement.setString(1, userName);
				prepStatement.setString(2, intSound.toString());
				prepStatement.setString(3, livesNumber.toString());
				prepStatement.setString(4, ammoNumber.toString());
				prepStatement.setString(5, rowNumber.toString());
				prepStatement.setString(6, columnNumber.toString());
				prepStatement.setString(7, ballSpeed.toString());
				prepStatement.setString(8, batSpeed.toString());
				prepStatement.setString(9, bonusSpeed.toString());
				prepStatement.setString(10, percent.percentNumberToInteger().toString());
			}
			prepStatement.executeUpdate();
			prepStatement.close();
			connection.close();
		} catch (SQLException e) {
			System.out.println(e.getMessage());
		}
	}
	
	/** Return the brick breaker configuration for the current loggerUser.
	 * @param userName : The current logged user's nick name.
	 * @return a list of object with a full screen boolean, a resolution string and a decorated boolean.
	 * @throws NoSaveException if the select of the logged user returns a closed result set.
	 */
	public static ArrayList<Object> load(String userName) throws NoSaveException {
		ArrayList<Object> config = new ArrayList<Object>();
		try{
			Connection connection = DataConnection.openConection();
			PreparedStatement prepStatement = connection.prepareStatement("Select * from BBCONFIG where nick = ?;");
			prepStatement.setString(1, userName);
			ResultSet resultSet = prepStatement.executeQuery();
			if(resultSet.next()) {
				config.add(Utilities.intToBoolean(resultSet.getInt("sound")));
				config.add(resultSet.getInt("liveNumber"));
				config.add(resultSet.getInt("ammoNumber"));
				config.add(resultSet.getInt("rowNumber"));
				config.add(resultSet.getInt("columnNumber"));
				config.add(resultSet.getInt("ballSpeed"));
				config.add(resultSet.getInt("batSpeed"));
				config.add(resultSet.getInt("bonusSpeed"));
				config.add(PercentNumber.intToPercentNumber(resultSet.getInt("percent")));
				resultSet.close();
				prepStatement.close();
				connection.close();
			} else {
				resultSet.close();
				prepStatement.close();
				connection.close();
				throw new NoSaveException();
			}
		} catch(SQLException e) {
			
		}
		return config;
	}
}